<?php
namespace Api\Controller;

class CollectController extends ApiController
{

    /**
     * 收藏商品
     * @param int product_id
     */
    public function addProduct()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        params_exist(array(
            'product_id'
        ));

        $M_collect = M('collect_product');
        $product_id = I('post.product_id');

        !(preg_match('/[1-9]\d*$/', $product_id) ) && response(-1, '', '参数格式错误');

        !get_table_field($product_id,'id','id','product') && response(-2,'','商品不存在');

        $val_map=array('uid'=>API_UID,'product_id'=>$product_id);
        $M_collect->where($val_map)->find() && response(-1,'','此商品已经收藏');

        $map = array(
            'uid' => API_UID,
            'product_id' => $product_id,
            'create_time' => NOW_TIME
        );

        $result = $M_collect->add($map);
        $result ? response(1) : response(- 2);
    }

    /**
     * 收藏的商品列表
     *
     * @param int Page
     * @param int page_size
     *
     *
     */
    public function collectProducts()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        $page = I('post.page', 1);
        $page_size = I('post.page_size', 5);
        $offset = ($page - 1) * $page_size;

        $M_product_image = M('product_image');
        $M_collect = M('collect_product');
        $map = array();
        $map['uid'] = API_UID;

        $list = $M_collect->limit($offset, $page_size)
            ->where($map)
            ->select();
        empty($list) && response(-2);
        foreach($list as &$item){
            $item['product_title'] = get_table_field($item['product_id'], 'id', 'title', 'product');
            $item['product_price'] = get_table_field($item['product_id'], 'id', 'price', 'product');
            $item['use_score'] = get_table_field($item['product_id'], 'id', 'use_score', 'product');
            $item['image']= get_url(get_table_field($item['product_id'],'product_id','cover_id','product_image'));
        }
        $total=$M_collect->where($map)
            ->count();
        $result=array();

        foreach ($list as $k => $v) {
            if($v['product_price']){
                $list[$k]['use_score']='0';
            }else{
                $list[$k]['use_score']=$v['use_score'];
            }
        }
        $result['result']=$list;
        $result['total']=$total;
        response(1, $result);
    }

    /**
     * 删除收藏的商品
     *
     * @param int id
     */
    public function productCollectDel()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'id'
        ));

        $id = I('post.id');
        $M_collect = M('collect_product');

        !(preg_match('/[1-9]\d*$/', $id) ) && response(-1, '', '参数格式错误');

        !$M_collect->where(array('id'=>$id,'uid'=>API_UID))->find()   && response(-2,'','收藏不存在');

        $result = $M_collect->where(array(
            'id' => $id,
            'uid' =>API_UID
        ))->delete();

        $result ? response(1) : response(- 9);
    }

    /**
     * 根据product_id 获取收藏id
     * @param int product_id
     */
    public function getCollectIdByProduct(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'product_id'
        ));

        $product_id=I('post.product_id');
        !(preg_match('/^[1-9]\d*$/', $product_id) ) && response(-1, '', '参数格式错误');
        !get_table_field($product_id,'id','id','product') && response(-2,'','商品不存在');

        //$collledt_id=get_table_field($product_id,'product_id','id','collect_product');
        $map['uid']= API_UID;
        $map['product_id']=$product_id;
        $collledt_id=M('collect_product')->where($map)->getField('id');
        $collledt_id ? response(1,$collledt_id) : response(-2,0) ;
    }

    /**
     * 根据shop_id 获取收藏id
     * @param int shop_id
     */
    public function getCollectIdByShop(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'shop_id'
        ));

        $shop_id=I('post.shop_id');
        !(preg_match('/^[1-9]\d*$/', $shop_id) ) && response(-1, '', '参数格式错误');
        !get_table_field($shop_id,'id','id','shop') && response(-2,'','店铺不存在');


        //$collledt_id=get_table_field($shop_id,'shop_id','id','collect_shop');
        $map['uid']= API_UID;
        $map['shop_id']=$shop_id;
        $collledt_id=M('collect_shop')->where($map)->getField('id');
        $collledt_id ? response(1,$collledt_id) : response(-2,0);
    }
    /**
     * 收藏店铺
     * @param int shop_id
     */
    public function addShop()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        params_exist(array(
            'shop_id'
        ));
        $M_collect = M('collect_shop');
        $shop_id = I('post.shop_id');
        !(preg_match('/[1-9]\d*$/', $shop_id) ) && response(-1, '', '参数格式错误');
        !get_table_field($shop_id,'id','id','shop') && response(-2,'','店铺不存在');

        $val_map=array('uid'=>API_UID,'shop_id'=>$shop_id);
        $M_collect->where($val_map)->find() && response(-1,'','店铺已经收藏');

        $map = array(
            'uid' => API_UID,
            'shop_id' => $shop_id,
            'create_time' => NOW_TIME
        );

        $result = $M_collect->add($map);
        $result ? response(1) : response(- 2);
    }

    /**
     * 收藏的店铺 列表
     * @param int Page
     * @param int page_size
     */
    public function collectShops()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        $page = I('post.page', 1);
        $page_size = I('post.page_size', 5);
        $offset = ($page - 1) * $page_size;

        $M_collect = M('collect_shop');
        $map = array();
        $map['uid'] = API_UID;

        $list = $M_collect->limit($offset, $page_size)
            ->where($map)
            ->select();

        empty($list) && response(- 2);

        foreach ($list as &$item) {
            $item['shop_title'] = get_table_field($item['shop_id'], 'id', 'title', 'shop');
            $item['headpic_path'] = get_url(get_table_field($item['shop_id'], 'id', 'headpic', 'shop'));
        }

        $total=$M_collect->where($map)
            ->count();
        $result=array();
        $result['result']=$list;
        $result['total']=$total;
        response(1, $result);
    }

    /**
     * 删除收藏的店铺
     * @param  int id
     */
    public function shopCollectDel()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        params_exist(array(
            'id'
        ));
        $id = I('post.id');

        !(preg_match('/[1-9]\d*$/', $id) ) && response(-1, '', '参数格式错误');

        $M_collect = M('collect_shop');
        $res= $M_collect->where(array('id'=>$id,'uid'=>API_UID))->find();

        if($res){
            $result = $M_collect->where(array(
                'id' => $id,
                'uid' =>API_UID
            ))->delete();
        }else{
            response(-2,'','收藏不存在');
        }

        $result ? response(1) : response(-9);
    }

    /**
     * 我的足迹
     * @param int Page
     * @param int page_size
     */
    public function myBrowse(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        $page = I('post.page', 1);
        $page_size = I('post.page_size', 10);
        $offset = ($page - 1) * $page_size;


        $M_collect = M('collect_browse');
        $map = array();
        $map['uid'] = API_UID;

        $list = $M_collect->limit($offset, $page_size)
            ->where($map)
            ->order('create_time desc')
            ->select();

        empty($list) && response(- 2,'无数据');

        foreach ($list as &$item) {
            $item['product_title'] = get_table_field($item['product_id'], 'id', 'title', 'product');
            $item['product_price'] = get_table_field($item['product_id'], 'id', 'price', 'product');
            if($item['product_price']){
                $item['use_score']=0;
            }else{
                $item['use_score']=get_table_field($item['product_id'], 'id', 'use_score', 'product');
            }

            $item['image']= get_url(get_table_field($item['product_id'],'product_id','cover_id','product_image'));
        }




        $total=$M_collect->where($map)->count();
        $result=array();
        $result['result']=$list;
        $result['total']=$total;
        response(1, $result);
    }

    /**
     * 删除我的足迹
     *
     * @param int id
     */
    public function borwseCollectDel()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'id'
        ));

        $id = I('post.id');
        $M_collect = M('collect_browse');

        !(preg_match('/[1-9]\d*$/', $id) ) && response(-1, '', '参数格式错误');

        !$M_collect->where(array('id'=>$id,'uid'=>API_UID))->find()   && response(-2,'','忘记不存在');

        $result = $M_collect->where(array(
            'id' => $id
        ))->delete();

        $result ? response(1) : response(- 9);
    }
}
